﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class SearchResult : System.Web.UI.Page
{
    public int MethodSearch { get; set; }
    public int CompanyId { get; set; }
    public string KeyWord { get; set; }
    public float PriceFrom { get; set; }
    public float PriceTo { get; set; }
    public int TypeSearch { get; set; }
    private SqlDataReader DrPhone = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        MethodSearch = int.Parse(Request.QueryString["MethodSearch"].ToString());
        CompanyId = int.Parse(Request.QueryString["CompanyId"].ToString());
        KeyWord = Request.QueryString["KeyWord"].ToString();
        PriceFrom = float.Parse(Request.QueryString["PriceFrom"].ToString());
        PriceTo = float.Parse(Request.QueryString["PriceTo"].ToString());
        TypeSearch = int.Parse(Request.QueryString["TypeSearch"].ToString());

        if (MethodSearch == 0)
        {
            GetResultByPhone();
        }
        else
        {
            GetResultByCompany();
        }
        lvShowAllPhones.DataSource = DrPhone;
        lvShowAllPhones.DataBind();

    }

    public void GetResultByPhone()
    {
        Connection.cmd = new System.Data.SqlClient.SqlCommand("spSearchInAllPhones", Connection.GetConn());
        Connection.cmd.CommandType = System.Data.CommandType.StoredProcedure;
        Connection.cmd.Parameters.AddWithValue("@keyWord", KeyWord);
        Connection.cmd.Parameters.AddWithValue("@priceFrom", PriceFrom);
        Connection.cmd.Parameters.AddWithValue("@priceTo", PriceTo);
        Connection.cmd.Parameters.AddWithValue("@type", TypeSearch);
        DrPhone = Connection.cmd.ExecuteReader();
    }

    public void GetResultByCompany()
    {
        Connection.cmd = new System.Data.SqlClient.SqlCommand("spSearchByCompanyId", Connection.GetConn());
        Connection.cmd.CommandType = System.Data.CommandType.StoredProcedure;
        Connection.cmd.Parameters.AddWithValue("@companyId", CompanyId);
        Connection.cmd.Parameters.AddWithValue("@keyWord", KeyWord);
        Connection.cmd.Parameters.AddWithValue("@priceFrom", PriceFrom);
        Connection.cmd.Parameters.AddWithValue("@priceTo", PriceTo);
        Connection.cmd.Parameters.AddWithValue("@type", TypeSearch);
        DrPhone = Connection.cmd.ExecuteReader();
    }
}